package com.util;

import org.springframework.data.redis.core.RedisTemplate;

import java.io.Serializable;
import java.util.List;

/**
 * Redis工具类：字符串
 */
public class RedisUtil {
    private RedisTemplate<Serializable,Object> redisTemplate;
    /**
     * 写入缓存:字符串
     */
    public boolean set(final String key,String value){
        boolean result=false;
        try{
            redisTemplate.opsForValue().set(key,value);
            result=true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 读取缓存：字符串
     */
    public Object get(final String key){
        Object result=null;
        try {
            result=redisTemplate.opsForValue().get(key);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 写入缓存：列表
     * @param key
     * @param value
     * @return
     */
    public boolean lpush(final String key,Object value){
        boolean result=false;
        try{
            redisTemplate.opsForList().leftPush(key,value);
            result=true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 读取缓存：列表
     * @param key
     * @param l
     * @param l1
     * @return
     */
    public List<Object> lrange(final String key,long l,long l1){
        List<Object> result=null;
        try{
            result=redisTemplate.opsForList().range(key,l,l1);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }
    public void setRedisTemplate(RedisTemplate<Serializable,Object> redisTemplate) {
        this.redisTemplate=redisTemplate;
    }

}

